package net.bbtang.website.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bbtang.website.pojo.ScoreInfoDTO;
import net.bbtang.website.pojo.ScoreInfoRefDTO;
import net.bbtang.website.service.ScoreInfoRefService;
import net.bbtang.website.service.ScoreInfoService;
import net.bbtang.website.utils.ConstantsStatus;
import net.bbtang.website.utils.StatusCode;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/scoreInfo")
public class ScoreInfoApiController {
	@Autowired
	ScoreInfoService scoreInfoService;
	@Autowired
	ScoreInfoRefService scoreInfoRefService;
	
	private static final Logger logger = Logger.getLogger(ScoreInfoApiController.class); 
	
	/**获取用户积分信息
	 @api {post} /scoreInfo/getScoreInfo 获取用户积分信息
	 
	 @apiParam {int} userId 用户id 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"userId":1,
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
	  "number": "2000",
	  "flag": true,
	  "data": {
	    "id": 3,个人积分标识
	    "userId": 111,用户id
	    "score": 100,总积分
	    "useState": 0,用户积分状态 0：正常 1：停用
	    "createTime": 1492093771000,创建时间
	    "updateTime": 1492093774000    更新时间
	  }
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取账户积分信息异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getScoreInfo", method = RequestMethod.POST)
	@ResponseBody
	public Object getScoreInfo(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			String userId = jb.getString("userId");
			Map<String,Object> map =new HashMap<String, Object>();
			map.put("userId", userId);
			ScoreInfoDTO scoreInfo = scoreInfoService.getScoreInfo(map);
			sc.setFlag(true);
			sc.setData(scoreInfo);;
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取账户积分信息异常");
		}
		
		return sc;
	
	}
	
	/**账户积分明细
	 @api {post} /scoreInfo/getScoreInfoDetail 账户积分明细
	 
	 @apiParam {int} scoreId 积分账户id 必填

	 @apiSuccess {boolean} flag 请求状态，码状态   成功：true 失败：false
	 @apiSuccess {String} num 状态码值
	 @apiSuccess {String} detailMsg 状态码详细值
	 @apiSuccess {Object} data 返回数据
	
	 @apiParamExample {json} 请求参数Demo:
	 {
		"scoreId":1,
	}

	@apiSuccessExample {json} 请求成功结果Demo:
	{
		 "number": "2000",
		  "flag": true,
		  "data": [
		    {
		      "id": 3,  个人积分详情标识
		      "scoreId": 3,  积分id
		      "scoreState": 0,  积分状态：0，增加 1，减少
		      "scoreType": 1,   积分类型： 增加 ：1：签到 2：订单完成 3：成就 4：其他  /减少：1：打水 2：美食 3：拍照
		      "changeScore": 50,  积分
		      "createTime": 1492093878000,  创建时间
		      "updateTime": 1492093881000  更新时间
		    },
		    {
		      "id": 4,
		      "scoreId": 3,
		      "scoreState": 0,
		      "scoreType": 1,
		      "changeScore": 50,
		      "createTime": 1492093892000,
		      "updateTime": 1492093894000
		    }
		  ]
	}

	@apiErrorExample {json} 请求成功得异常结果
	HTTP/1.1 5000 error
	{
	  "number": "5000",
	  "flag": false,
	  "detailMsg": "获取账户积分信息异常"
	}

	@apiErrorExample  请求参数异常
		HTTP/1.1 400
	@apiErrorExample  请求数据类型异常
	HTTP/1.1 415
	@author wxw
	 */
	@RequestMapping(value = "/getScoreInfoDetail", method = RequestMethod.POST)
	@ResponseBody
	public Object getScoreInfoDetail(@RequestBody Object body,HttpServletRequest request,HttpServletResponse response) {
		StatusCode<Object> sc=new StatusCode<Object>(ConstantsStatus.SC2000);
		try {
			JSONObject jb = JSONObject.fromObject(body);
			String scoreId = jb.getString("scoreId");
			Map<String,Object> map =new HashMap<String, Object>();
			map.put("scoreId", scoreId);
			List<ScoreInfoRefDTO> scoreInfoReg = scoreInfoRefService.getScoreInfoDetail(map);
			sc.setFlag(true);
			sc.setData(scoreInfoReg);;
		} catch (Exception e) {
			e.printStackTrace();
			sc.codes(ConstantsStatus.SC5000);
			sc.setDetailMsg("获取账户积分明细异常");
		}
		
		return sc;
	
	}
	
	
	
	
	
	
	
}
